package org.dydl.controller.system;

import org.dydl.common.dto.ResponseMessage;
import org.dydl.model.system.MaterialsPurchase;
import org.dydl.model.system.MaterialsPurchaseDetail;
import org.dydl.model.system.vo.PurchaseOrderVo;
import org.dydl.service.system.IGoodsService;
import org.dydl.service.system.MaterialsPurchaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 采购单
 */

@RestController
@RequestMapping("/api/materialsPurchase")
public class MaterialsPurchaseController {
	@Autowired
	private MaterialsPurchaseService materialsPurchaseService; 
	@Autowired
	private IGoodsService goodsService;

	
	@RequestMapping("/list")
	public ResponseMessage list(int page,int rows,String purchaseSingleNo){
		
		ResponseMessage res = materialsPurchaseService.list(page,rows,purchaseSingleNo);
		
		return res;
	}
	
	@RequestMapping("/save")
	public ResponseMessage save(@RequestBody MaterialsPurchase materialsPurchase){
		
		return materialsPurchaseService.save(materialsPurchase);	
		
	}

	/**
	 * 到货确认接口
	 * @return
	 */
	@RequestMapping("/affirm")
	public ResponseMessage affirmMaterisals(@RequestBody String[] purchaseSingleNos){
		return materialsPurchaseService.affirmMaterisals(purchaseSingleNos);
	}
	
	

	@RequestMapping("/delete")
	public boolean delete(int[] ids){
		if (ids.length != 0) {
			for (int i = 0; i < ids.length; i++) {
				materialsPurchaseService.delete(ids[i]);
			}
			return true;
		} else {
			return false;
		}
		
	}

	/**
	 * 生成采购单号
	 * @return
	 */
	@RequestMapping("/getPurchaseSingleNo")
	public String getDeliveryNoteNo(){
		
		String chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
		
		char random = chars.charAt((int)(Math.random()*26));
		
		String purchaseSingleNo = random+""+ System.currentTimeMillis();
		
		return purchaseSingleNo;
	}
	
	@RequestMapping("/findByPurchaseSingleNo")
	public MaterialsPurchase findByPurchaseSingleNo(String purchaseSingleNo){
		
		return materialsPurchaseService.findByPurchaseSingleNo(purchaseSingleNo);
		
	}
	
	@RequestMapping("/findDetailByPurchaseSingleNo")
	public List<MaterialsPurchaseDetail> findDetailByPurchaseSingleNo(String purchaseSingleNo){
		return materialsPurchaseService.findDetailByPurchaseSingleNo(purchaseSingleNo);
		
	}
	
/*    *//**
     * 自动匹配商品
     *//*
    @RequestMapping("/getGoodsByName")
    public ResponseMessage getGoodsByName(@RequestParam(value = "name", required = false) String name) {
    	ResponseMessage res =null;
        if (!name.isEmpty()) {
             res = goodsService.getGoodsByName(name);
        }
        return res;
    }*/

	/**
	 * 保存原料采购单
	 * @param PurchaseOrderVo
	 * @return ResponseMessage
	 */
	@RequestMapping(value = "/savePurchaseOrder",method = RequestMethod.POST)
	public ResponseMessage savePurchaseOrder(@RequestBody PurchaseOrderVo PurchaseOrderVo) {
		return materialsPurchaseService.savePurchaseOrder(PurchaseOrderVo);
	}


    @RequestMapping("/saveDetail")
    public void saveDetail(@RequestBody List<MaterialsPurchaseDetail> grid_arr){
    	materialsPurchaseService.saveDetail(grid_arr);
    }
	
    @RequestMapping("/deleteDetail")
    public void deleteDetail(@RequestParam(value = "purchaseSingleNo", required = false) String purchaseSingleNo){
    	materialsPurchaseService.deleteDetail(purchaseSingleNo);
    }
    
    @RequestMapping("/changeExamine")
    public void changeExamine(@RequestParam(value = "ids", required = false)Integer[] ids) {
    	if (ids.length != 0) {
			for (int i = 0; i < ids.length; i++) {
				materialsPurchaseService.changeExamine(ids[i],10);
			}
		} 
	}

}
